package com.malty.gateway.config;

import com.malty.common.domain.entity.UserInfo;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;

/**
 * @author malty
 */
@Data
@NoArgsConstructor
public class GatewayContext {
    private Long userId;
    private Long apiId;
    private String accessKey;
    private String secretKey;

    public static GatewayContext generateContext(ServerHttpRequest request, UserInfo user, Long apiId) {
        GatewayContext context = new GatewayContext();
        context.setUserId(user.getId());
        context.setSecretKey(user.getSecretKey());
        context.setApiId(apiId);

        HttpHeaders headers = request.getHeaders();
        String accessKey = headers.getFirst("access-key");
        context.setAccessKey(accessKey);
        return context;
    }
}
